package com.geniusk.system.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.geniusk.common.core.JSONResult;
import com.geniusk.common.utils.PageUtil;
import com.geniusk.file.utils.MinioUtil;
import com.geniusk.security.annotation.PreFuncAuth;
import com.geniusk.security.utils.SecurityUtil;
import com.geniusk.system.model.bo.app.*;
import com.geniusk.system.model.pojo.App;
import com.geniusk.system.service.AppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 应用管理接口
 */
@RestController
@RequestMapping("system/app")
public class AppController {

    @Autowired
    private AppService appService;

    @Autowired
    private MinioUtil minioUtil;

    /**
     * 获取最新应用下载地址
     */
    @GetMapping("/selectLatestApp")
    public JSONResult selectLatestAppUrl() {
        String fileUrl = appService.selectLatestAppUrl();
        Map<String, Object> result = new HashMap<>();
        String appUrl = minioUtil.getFileUrl(fileUrl);
        result.put("downloadUrl",appUrl);
        return JSONResult.ok(result);
    }

    /**
     * 根据ID查询查询应用
     */
    @PostMapping("/selectAppById")
    @PreFuncAuth("SYS:APP:SELECT")
    public JSONResult selectAppById(@Valid @RequestBody SelectAppByIdBO selectAppByIdBO) {
        App app = appService.selectAppById(selectAppByIdBO.getAppId());
        Map<String, Object> result = new HashMap<>();
        result.put("app",app);
        return JSONResult.ok(result);
    }

    /**
     * 分页查询应用
     */
    @PostMapping("/selectAppByPage")
    @PreFuncAuth("SYS:APP:SELECT")
    public JSONResult selectDictByPage(@Valid @RequestBody SelectAppByPageBO selectAppByPageBO) {
        int pageNum = selectAppByPageBO.getPageNum();
        int pageSize = selectAppByPageBO.getPageSize();
        App app = JSONUtil.parse(selectAppByPageBO).toBean(App.class);
        app.setCreateBy(SecurityUtil.getUsername());
        PageUtil.startPage(pageNum,pageSize);
        List<App> appList= appService.selectAppByPage(app);
        Map<String,Object> result = PageUtil.getResultTableData(appList);
        return JSONResult.ok(result);
    }

    /**
     * 添加应用
     */
    @PostMapping("/insertApp")
    @PreFuncAuth("SYS:APP:INSERT")
    public JSONResult insertDict(@Valid @RequestBody InsertAppBO insertAppBO) {
        App app = JSONUtil.parse(insertAppBO).toBean(App.class);
        Long appId = IdUtil.getSnowflake().nextId();
        app.setAppId(appId);
        app.setCreateBy(SecurityUtil.getUsername());
        int rows = appService.insertApp(app);
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    /**
     * 修改应用
     */
    @RequestMapping("/updateApp")
    @PreFuncAuth("SYS:APP:UPDATE")
    public JSONResult updateApp(@Valid @RequestBody UpdateAppBO updateAppBO){
        App app = JSONUtil.parse(updateAppBO).toBean(App.class);
        app.setUpdateBy(SecurityUtil.getUsername());
        Map<String,Object> result = new HashMap<>();
        int rows = appService.updateApp(app);
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

    /**
     * 删除应用
     */
    @PostMapping("/deleteAppByIds")
    @PreFuncAuth("SYS:APP:DELETE")
    public JSONResult deleteAppByIds(@Valid @RequestBody DeleteAppByIdsBO deleteAppByIdsBO) {
        int rows = appService.deleteAppByIds(deleteAppByIdsBO.getAppIds());
        Map<String,Object> result = new HashMap<>();
        result.put("rows",rows);
        return JSONResult.ok(result);
    }

}
